/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.going.persistencia;

import co.com.going.modelo.Programa;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Olguita
 */
@Stateless
public class DAOprograma implements IDAOprograma {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public void crearPrograma(Programa programa) {
         try {
            
            entityManager.persist(programa);
            
        } catch (Exception e) {
            throw e;
            
        }
    }

    @Override
    public void eliminarPrograma(Programa programa) {
        
        try {
            
            entityManager.remove(programa);
            
        } catch (RuntimeException e) {
        
             throw e;
            
        }
    }

    @Override
    public void modificarPrograma(Programa programa) {
       
         try {
            
            entityManager.merge(programa);
            
        } catch (RuntimeException e) {
            
           throw e;
            
        }
    }

    @Override
    public List<Programa> programas() {
       
        try {
            
            String Jpql1="select p from Programa p";
            Query query=entityManager.createQuery(Jpql1);
            return query.getResultList();
            
            
        } catch (RuntimeException e) {
            
            throw e;
        }
    }

    
    @Override
    public Programa buscarProgramaPorNombre(String nombre) {
        
        try {
            
           String Jpql="select p from Programa p where p.nombrePrograma = '"+nombre+"'";
            Query query=entityManager.createQuery(Jpql);
            
            if(query.getResultList().isEmpty()){
            
                return null;
                
            }else{
            
                 return  (Programa) query.getResultList().get(0);
           
            }
                                   
        } catch (RuntimeException e) {
            
            throw e;
            
        }
        
                
    }

    @Override
    public Programa BuscarProgramaPorId(Long IdPrograma) {
        
        try {
            
            return entityManager.find(Programa.class, IdPrograma);
            
        } catch (RuntimeException e) {
            
            throw e;
            
        }
        
    }
    
}
